home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / xlib03.zip / XTEXT.ASM < prev    next >
Assembly Source File  |  1993-04-05  |  11KB  |  364 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XTEXT
  3. ;
  4. ; Point functions all MODE X 256 Color resolutions
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ; ****** Aeronautical Research Laboratory              ****************
  14. ; ****** Defence Science and Technology Organisation   ****************
  15. ; ****** Australia                                     ****************
  16. ;
  17. ; egg@dstos3.dsto.gov.au
  18. ; teg@bart.dsto.gov.au
  19. ;-----------------------------------------------------------------------
  20.  
  21.  
  22. include xlib.inc
  23. include xtext.inc
  24.  
  25. .data
  26.  
  27. _FontDriverActive db 0
  28.  
  29.  
  30. _CharHeight   db         0
  31. _CharWidth    db         0
  32. _FontPtr      dw  2 dup (0)
  33. _FirstChar    db         0
  34.  
  35. _UserFontPtr   dw  2 dup (0)
  36. _UserChHeight  db         0
  37. _UserChWidth   db         0
  38. _UserFirstCh   db         0
  39.  
  40.  
  41. F8x8Ptr       dw  2 dup (0)
  42. F8x14Ptr      dw  2 dup (0)
  43.  
  44. .code
  45.  
  46.  
  47. ; This is a look up table for the mirror image of a byte eg
  48. ; a byte with the value 11001010 has a corresponding byte in the table
  49. ; 01010011. This is necessary as the VGA rom font bits are the reverse
  50. ; order of what we need for the Mode X. If you know a better-faster way
  51. ; TELL ME!
  52.  
  53. MirrorTable  label byte
  54.     db   0,128, 64,192, 32,160, 96,224, 16,144, 80,208, 48,176,112,240
  55.     db   8,136, 72,200, 40,168,104,232, 24,152, 88,216, 56,184,120,248
  56.     db   4,132, 68,196, 36,164,100,228, 20,148, 84,212, 52,180,116,244
  57.     db  12,140, 76,204, 44,172,108,236, 28,156, 92,220, 60,188,124,252
  58.     db   2,130, 66,194, 34,162, 98,226, 18,146, 82,210, 50,178,114,242
  59.     db  10,138, 74,202, 42,170,106,234, 26,154, 90,218, 58,186,122,250
  60.     db   6,134, 70,198, 38,166,102,230, 22,150, 86,214, 54,182,118,246
  61.     db  14,142, 78,206, 46,174,110,238, 30,158, 94,222, 62,190,126,254
  62.     db   1,129, 65,193, 33,161, 97,225, 17,145, 81,209, 49,177,113,241
  63.     db   9,137, 73,201, 41,169,105,233, 25,153, 89,217, 57,185,121,249
  64.     db   5,133, 69,197, 37,165,101,229, 21,149, 85,213, 53,181,117,245
  65.     db  13,141, 77,205, 45,173,109,237, 29,157, 93,221, 61,189,125,253
  66.     db   3,131, 67,195, 35,163, 99,227, 19,147, 83,211, 51,179,115,243
  67.     db  11,139, 75,203, 43,171,107,235, 27,155, 91,219, 59,187,123,251
  68.     db   7,135, 71,199, 39,167,103,231, 23,151, 87,215, 55,183,119,247
  69.     db  15,143, 79,207, 47,175,111,239, 31,159, 95,223, 63,191,127,255
  70.  
  71. MirrorTableOffs dw         ?
  72.  
  73. ;----------------------------------------------------------------------
  74. ; x_text_init    - Initializes the Mode X text driver and sets the
  75. ;                  default font (VGA ROM 8x8)
  76. ;
  77. ; C caller:
  78. ;
  79. ;  x_text_init()
  80. ;
  81. ; Written by Themie Gouthas
  82. ;----------------------------------------------------------------------
  83. _x_text_init proc
  84.   push bp
  85.  
  86.   mov  [_FontDriverActive],TRUE
  87.   mov  ax,1130h                   ; AH = BIOS generator function
  88.                   ; AL = BIOS get font pointer subfunction
  89.   push ax                         ; Save Video interrupt function parameters
  90.   mov  bh,3                       ; Select 8x8 VGA ROM font
  91.   int  10h                        ; Call BIOS video interrupt
  92.   mov  word ptr [F8x8Ptr],bp      ; Save 8x8 Font address in FontPtr table
  93.   mov  word ptr [F8x8Ptr+2],es
  94.  
  95.   mov  word ptr [_FontPtr],bp     ; Default font = 8x8 ROM font
  96.   mov  word ptr [_FontPtr+2],es
  97.  
  98.   pop  ax                         ; Recall Video interrupt function parameters
  99.   mov  bh,2                       ; Select 8x14 VGA ROM font
  100.   int  10h                        ; Call BIOS video interrupt
  101.   mov  word ptr [F8x14Ptr],bp     ; Save 8x14 Font address in FontPtr table
  102.   mov  word ptr [F8x14Ptr+2],es
  103.  
  104.  
  105.   mov  al,8
  106.   mov  [_CharHeight],al            ; Set the font character heights
  107.   mov  [_CharWidth] ,al            ; Set the font character widths
  108.  
  109.   mov  dx,offset MirrorTable       ; Initialize mirror table offset
  110.   mov  cs:[MirrorTableOffs],dx
  111.  
  112.   pop  bp
  113.   ret
  114. _x_text_init endp
  115.  
  116.  
  117. ;----------------------------------------------------------------------
  118. ; x_set_font - Mode X Set current font for text drawing
  119. ;
  120. ; C caller:
  121. ;
  122. ;  x_set_font(int FontID)
  123. ;
  124. ; PARAMETERS  FontID    0 = VGA ROM 8x8
  125. ;                       1 = VGA ROM 8x14
  126. ;                       2 = User defined bitmapped font
  127. ;
  128. ;
  129. ; WARNING: A user font must be registered before setting FontID 2
  130. ;
  131. ; Written by Themie Gouthas
  132. ;----------------------------------------------------------------------
  133.  
  134. _x_set_font proc
  135.   ARG FontID:word
  136.   push bp
  137.   mov  bp,sp
  138.  
  139.   xor  dx,dx             ; Clear DX - Mirror table offset (0 for non ROM fonts)
  140.   mov  cx,FontID
  141.   cmp  cx,2
  142.  
  143.   jne  @@not_userfont     ; Do we have a user font
  144.   mov  ax,[_UserFontPtr]   ; Yes - Activate it
  145.   mov  [_FontPtr],ax
  146.  
  147.   mov  ax,[_UserFontPtr+2]
  148.   mov  [_FontPtr+2],ax
  149.  
  150.   mov  al,[_UserChHeight]
  151.   mov  [_CharHeight],al   ; Set the font character heights
  152.  
  153.   mov  al,[_UserChWidth]
  154.   mov  [_CharWidth],al    ; Set the font character heights
  155.  
  156.   mov  al,[_UserFirstCh]
  157.   mov  [_FirstChar],al
  158.   jmp  short @@done
  159.  
  160. @@not_userfont:              ; We have a ROM font
  161.  
  162.   mov  dx,offset MirrorTable
  163.   mov  [_CharWidth],8        ; Set the font character widths
  164.   mov  [_FirstChar],0        ; Character sets start at ascii 0
  165.   cmp  cx,1                  ; Do we have an 8x14 ROM font
  166.   jne  @@not_8x14font        ; No, we have 8x8 - jump
  167.  
  168.   mov  ax,[F8x14Ptr]         ; Yes Activate it
  169.   mov  [_FontPtr],ax
  170.  
  171.   mov  ax,[F8x14Ptr+2]
  172.   mov  [_FontPtr+2],ax
  173.  
  174.   mov  [_CharHeight],14    ; Set the font character heights
  175.   jmp  short @@done
  176.  
  177. @@not_8x14font:
  178.   mov  ax,[F8x8Ptr]        ; Activate the 8x8 ROM Font
  179.   mov  [_FontPtr],ax
  180.  
  181.   mov  ax,[F8x8Ptr+2]
  182.   mov  [_FontPtr+2],ax
  183.  
  184.   mov  [_CharHeight],8     ; Set the font character heights
  185.  
  186. @@done:
  187.   mov  cs:[MirrorTableOffs],dx
  188.  
  189.   pop  bp
  190.   ret
  191. _x_set_font endp
  192.  
  193.  
  194. ;----------------------------------------------------------------------
  195. ; x_register_userfont - Mode X register user font
  196. ;
  197. ; C caller:
  198. ;
  199. ;  x_register_userfont(void far *user_font)
  200. ;
  201. ;
  202. ; NOTES  registering a user font deregisters the previous user font
  203. ;        User fonts may be at most 8 pixels wide
  204. ;
  205. ;
  206. ; USER FONT STRUCTURE
  207. ;
  208. ;  Word:  ascii code of first char in font
  209. ;  Byte:  Height of chars in font
  210. ;  Byte:  Width of chars in font
  211. ;  n*h*Byte: the font data where n = number of chars and h = height
  212. ;      of chars
  213. ;
  214. ; WARNING: The onus is on the program to ensure that all characters
  215. ;          drawn whilst this font is active, are within the range of
  216. ;          characters defined.
  217. ;
  218. ; Written by Themie Gouthas
  219. ;----------------------------------------------------------------------
  220. _x_register_userfont proc
  221.   ARG  FontToRegister:dword
  222.   push bp
  223.   mov  bp,sp
  224.   push si
  225.  
  226.   mov  ax,word ptr [FontToRegister]
  227.   mov  bx,word ptr [FontToRegister+2]
  228.   add  ax,4
  229.   mov  [_UserFontPtr],ax
  230.   mov  [_UserFontPtr+2],bx
  231.  
  232.   push ds
  233.   lds  si,[FontToRegister]
  234.   lodsw
  235.   mov  bx,ax
  236.   lodsw
  237.   pop  ds
  238.  
  239.   mov  [_UserChHeight],al
  240.   mov  [_UserChWidth],ah
  241.   mov  [_UserFirstCh],bl
  242.   pop  si
  243.   pop  bp
  244.   ret
  245. _x_register_userfont endp
  246.  
  247.  
  248. ;----------------------------------------------------------------------
  249. ; x_char_put - Mode X Draw a text character at the specified location
  250. ;
  251. ;
  252. ; C caller:
  253. ;
  254. ;  x_char_put(char ch, int x, int y, unsigned ScrnOffs, unsigned Color)
  255. ;
  256. ; PARAMETERS  ch        char to draw
  257. ;             x,y       screen coords at which to draw ch
  258. ;             ScrnOffs  Starting offset of page on whih to draw
  259. ;          Color     Color of the text
  260. ;
  261. ; NOTES:  Uses the current font settings. See SetFont, InitTextDriver,
  262. ;         RegisterUserFont
  263. ;
  264. ; WARNING: InitTextDriver must be called before using this function
  265. ;
  266. ;
  267. ; Written by Themie Gouthas
  268. ;----------------------------------------------------------------------
  269. _x_char_put  proc
  270.   ARG  Chr:byte,X:word,Y:word,ScrnOffs:word,Color:word
  271.   LOCAL ScreenInc:word,Hold:word=LocalStk
  272.   push bp
  273.   mov  bp,sp
  274.   sub  sp,LocalStk
  275.   push si
  276.   push di
  277.   push ds
  278.  
  279.   cld
  280.   mov  ax,[_ScrnLogicalByteWidth] ; AX = Virtual screen width
  281.   mov  bx,ax                      ; copy Virt screen width and decrement
  282.   sub  bx,3                       ; by the max number of bytes (whole or part)
  283.                   ; that a character row may occupy on the screen
  284.   mov  [ScreenInc],bx             ; Save it to the local stack var. SceenInc
  285.   mul  [Y]                        ; Find the starting dest. screen address of
  286.   mov  di,[X]                     ;  the character to draw
  287.   mov  cx,di
  288.   shr  di,2
  289.   add  di,ax
  290.   add  di,[ScrnOffs]              ; Dont forget to compensate for page
  291.  
  292.   mov  ax,SCREEN_SEG              ; ES:DI -> first screen dest. byte of char
  293.   mov  es,ax
  294.  
  295.   and  cx,3                       ; CH = 0, CL = Plane of first pixel
  296.  
  297.   mov  bx,cs:[MirrorTableOffs]    ; set BX to offset of mirror table for XLAT
  298.   mov  al,[_CharHeight]           ; AL = Character height, AH = 0
  299.   xor  ah,ah
  300.   mov  ch,al                      ; CL = Character height
  301.   mov  dl,[Chr]                   ; User fonts may have incomplete charsets
  302.   sub  dl,[_FirstChar]            ;  this compensates for fonts not starting at
  303.                   ;  ascii value 0
  304.   mul  dl                         ; Mult AX by character to draw giving offset
  305.                   ; of first character byte in font table
  306.  
  307.   lds  si,dword ptr [_FontPtr]    ; DS:SI -> beggining of required font
  308.   add  si,ax                      ; DS:SI -> first byte of req. char
  309.  
  310.   mov  dx,SC_INDEX                ; Prepare for VGA out's
  311.  
  312. @@MainLoop:
  313.  
  314.   lodsb               ; load character byte into AL
  315.   or   bx,bx          ; if BX=0 -> User font, so no need to mirror data
  316.   jz   @@DontMirror
  317.   push ds
  318.   mov  dx,cs          ; Set DS to the Mirror lookup table's segment
  319.   mov  ds,dx          ; - BX should already contain the offset addr of table
  320.   xlatb               ; AL is now replaced by the corresponding table entry
  321.   pop  ds             ; Restore previous data segment
  322.   mov  dx,SC_INDEX    ; Restore DX
  323.  
  324. @@DontMirror:
  325.   xor  ah,ah          ; shift the byte for the dest plane and save it
  326.   shl  ax,cl
  327.   mov  [Hold],ax
  328.  
  329.   mov  ah,al                 ; output high nibble of first byte of shifted char
  330.   mov  al,MAP_MASK
  331.   out  dx,ax
  332.   mov  al,byte ptr [Color]
  333.   stosb
  334.                  ; output low nibble of first byte of shifted char
  335.   mov  ax,[Hold]
  336.   shl  ax,4
  337.   mov  al,MAP_MASK
  338.   out  dx,ax
  339.   mov  al,byte ptr [Color]
  340.   stosb
  341.  
  342.   mov  ax,[Hold]             ; output high nibble of last byte of shifted char
  343.   mov  al,MAP_MASK           ; completing the drawing of one character row
  344.   out  dx,ax
  345.   mov  al,byte ptr [Color]
  346.   stosb
  347.  
  348.   add  di,[ScreenInc]        ; Now move to the next screen row and do the same
  349.   dec  ch                    ; any remaining character bytes
  350.   jnz  @@MainLoop
  351.   pop  ds
  352.   mov  ah,0
  353.   mov  al,[_CharWidth]       ; return the character width (for string fuctions
  354.   pop  di                    ; using this character drawing function).
  355.   pop  si
  356.   mov  sp,bp
  357.   pop  bp
  358.   ret
  359. _x_char_put endp
  360.  
  361.  
  362. end
  363.  
  364.